package com.honglang.module.article.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.honglang.aop.impl.UpdateRSSPointCut;
import com.honglang.aop.impl.UpdateSearchIndexPointCut;
import com.honglang.aop.impl.UpdateSideBarHtmlPointCut;
import com.honglang.common.tools.Constants;
import com.honglang.module.article.entity.BlogArticle;
import com.honglang.module.article.service.ArticleCategoriesService;
import com.honglang.module.article.service.BlogArticleManager;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class BlogArticleManagerAction extends BaseAction {
	
	private ArticleCategoriesService articleCategoriesService;
	
	private BlogArticleManager blogArticleManager;
	
	

	/**
	 * do something before writing articles;
	 * @return
	 */
	public String preArticleWrite(){
		
	    this.getRequest().setAttribute("artcleCategories", articleCategoriesService.getAll());
		return Action.SUCCESS;
	}
	
	
	/**
	 * before edit article
	 * @return
	 */
	public String preUpdateArticle(){
		Integer blogArticleId = Integer.valueOf(this.getRequest().getParameter("bid"));
		BlogArticle blogArticle = this.blogArticleManager.getBlogAritcleById(blogArticleId);
		this.getRequest().setAttribute("artcleCategories", articleCategoriesService.getAll());
		this.getRequest().setAttribute("title", blogArticle.getArticle().getTitle());
		this.getRequest().setAttribute("articleCategoryId", blogArticle.getArticle().getArticleCategories().getId());
		this.getRequest().setAttribute("keyWords", blogArticle.getArticle().returnKeyWordString());
		this.getRequest().setAttribute("articleSource", blogArticle.getArticle().getArticleSource());
		this.getRequest().setAttribute("articleContent", blogArticle.getArticle().getContent());
		this.getRequest().setAttribute("blogArticle", blogArticle);
		return Action.SUCCESS;
	}
	
	
	/**
	 * action of saving a blogArticle 
	 * @return
	 * @throws IOException 
	 */
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	@UpdateRSSPointCut
	public String saveBlogArticle() throws IOException{
		Map<String,String>	map = new HashMap<String,String>();
		map.put(Constants.Article_Category_ID, this.getRequest().getParameter("articleCategoryId"));
		map.put(Constants.Article_Title, this.getRequest().getParameter("title"));
		map.put(Constants.Article_KeyWords,  this.getRequest().getParameter("keyWords"));
		map.put(Constants.Article_Source,  this.getRequest().getParameter("articleSource"));
		map.put(Constants.Article_Content,  this.getRequest().getParameter("articleContent"));
		map.put(Constants.Article_ArticlePreView, this.getRequest().getParameter("articlePreView"));
		map.put(Constants.Article_DisplayReadMore, this.getRequest().getParameter("displayReadMore"));
		map.put(Constants.Article_CommentOpen, this.getRequest().getParameter("commentOpen"));
		map.put(Constants.USERNAME_KEY, (String)this.getSession().getAttribute(Constants.USERNAME_KEY));
		this.blogArticleManager.saveBlogArticle(this.blogArticleManager.createBlogArtilce(map));
		this.getResponse().getWriter().write("add");
		return Action.NONE;
	}

	
	
	/**
	 * preView the blogArticle
	 * @return
	 * @throws IOException
	 */
	public String preViewBlogArticle() throws IOException{
		Map<String,String>	map = new HashMap<String,String>();
		map.put(Constants.Article_Category_ID, this.getRequest().getParameter("articleCategoryId"));
		map.put(Constants.Article_Title, this.getRequest().getParameter("title"));
		map.put(Constants.Article_KeyWords,  this.getRequest().getParameter("keyWords"));
		map.put(Constants.Article_Source,  this.getRequest().getParameter("articleSource"));
		map.put(Constants.Article_Content,  this.getRequest().getParameter("articleContent"));
		map.put(Constants.Article_ArticlePreView, this.getRequest().getParameter("articlePreView"));
		map.put(Constants.Article_DisplayReadMore, this.getRequest().getParameter("displayReadMore"));
		map.put(Constants.Article_CommentOpen, this.getRequest().getParameter("commentOpen"));
		map.put(Constants.USERNAME_KEY, (String)this.getSession().getAttribute(Constants.USERNAME_KEY));
		this.getRequest().setAttribute("blogArticle",this.blogArticleManager.createBlogArtilce(map));
		return Action.SUCCESS;
	}

	
	
	
	
	/**
	 * update the blogArticle
	 * @return
	 * @throws IOException
	 */
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	@UpdateRSSPointCut
	public String updateBlogArticle() throws IOException{
		Integer blogArticleId = Integer.valueOf(this.getRequest().getParameter("bid"));
		BlogArticle blogArticle = this.blogArticleManager.getBlogAritcleById(blogArticleId);
		Map<String,String>	map = new HashMap<String,String>();
		map.put(Constants.Article_Category_ID, this.getRequest().getParameter("articleCategoryId"));
		map.put(Constants.Article_Title, this.getRequest().getParameter("title"));
		map.put(Constants.Article_KeyWords,  this.getRequest().getParameter("keyWords"));
		map.put(Constants.Article_Source,  this.getRequest().getParameter("articleSource"));
		map.put(Constants.Article_Content,  this.getRequest().getParameter("articleContent"));
		map.put(Constants.Article_ArticlePreView, this.getRequest().getParameter("articlePreView"));
		map.put(Constants.Article_DisplayReadMore, this.getRequest().getParameter("displayReadMore"));
		map.put(Constants.Article_CommentOpen, this.getRequest().getParameter("commentOpen"));
		map.put(Constants.USERNAME_KEY, (String)this.getSession().getAttribute(Constants.USERNAME_KEY));
		this.blogArticleManager.saveBlogArticle(this.blogArticleManager.updateBlogArtilce(map,blogArticle));
		this.getResponse().getWriter().write("update");
		return Action.NONE;
	}
	
	
	/**
	 * prepare article manager;
	 * @return
	 */
	public String preArticleManager(){
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(pageNoStr);
		}
		this.getRequest().setAttribute("pager", this.blogArticleManager.getBlogArticleInPager(Constants.pageSize, pageNo));
		//this.getRequest().setAttribute("articleList", this.blogArticleManager.getAllBlogArticle());
		return	Action.SUCCESS;
	}
	
	
	
	/**
	 * delete blogArticle by id;
	 * @return
	 */
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	@UpdateRSSPointCut
	public String deleteBlogArticle(){
		String blogArticleIds =this.getRequest().getParameter("bid");
		if(null != blogArticleIds && !blogArticleIds.isEmpty()){
			if(blogArticleIds.split(Constants.blogArticle_Split).length == 1){
				this.getBlogArticleManager().deleteBlogArticle(Integer.valueOf(blogArticleIds));
			}else{
				this.getBlogArticleManager().batchDeleteBlogArticle(blogArticleIds);
			}
		}
		return Action.NONE;
	}
	
	/**
	 * prepare to manager article Comments;
	 * @return
	 */
	public String preArticleCommentManager(){
		String articleId = this.getRequest().getParameter("bid");
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int commentPageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			commentPageNo = Integer.valueOf(pageNoStr);
		}
		Map<String,Object> map = this.getBlogArticleManager().getBlogArticleCommentPerPage(Constants.pageCommentSize,commentPageNo , Integer.valueOf(articleId));
		BlogArticle blogArticle =  this.blogArticleManager.getBlogAritcleById(Integer.valueOf(articleId));
		this.getRequest().setAttribute("blogArticle", blogArticle);
		this.getRequest().setAttribute("preArticle", this.getBlogArticleManager().getPreBlogArticle(blogArticle));
		this.getRequest().setAttribute("nextArticle", this.getBlogArticleManager().getNextBlogArticle(blogArticle));
		this.getRequest().setAttribute("pager", map.get("pager"));
		this.getRequest().setAttribute("commentList", map.get("commentList"));
		return Action.SUCCESS;
	}
	
	
	
	/**
	 * revert the display property of articleComment;
	 * @return
	 */
	public String updateArticleComment(){
		String blogArticleCommentId = this.getRequest().getParameter(Constants.BE_COMMENTED_ID);
		if(null != blogArticleCommentId){
			this.getBlogArticleManager().revertCommentDisplay(Integer.valueOf(blogArticleCommentId));
		}
		return Action.NONE;
	}
	
	
	
	
	
	public ArticleCategoriesService getArticleCategoriesService() {
		return articleCategoriesService;
	}

	public void setArticleCategoriesService(
			ArticleCategoriesService articleCategoriesService) {
		this.articleCategoriesService = articleCategoriesService;
	}

	public BlogArticleManager getBlogArticleManager() {
		return blogArticleManager;
	}

	public void setBlogArticleManager(BlogArticleManager blogArticleManager) {
		this.blogArticleManager = blogArticleManager;
	}
	
	
	

}
